<template>
	<div class="main">
		<div class="main-header" style="display: flex;justify-content: space-between;">
			<div style="display: flex;flex:1;flex-wrap: wrap;">
				<el-input v-model="searchParam.search" placeholder="请输入用户名称"
					style="width:200px;margin-right: 10px;"></el-input>
				<div style="width:340px;margin-right:10px;" class="ml-10">
					<el-date-picker clearable v-model="searchParam.search_time" value-format="timestamp" type="daterange"
						range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
					</el-date-picker>
				</div>
				<shequSelect v-model="searchParamCp.community_id" style="width: 180px;margin-right: 10px;"></shequSelect>
				<el-select v-model="searchParam.type" placeholder="请选择积分类型" style="width:200px;margin-right: 10px;">
					<el-option v-for="(item, index) in typeList" :key="index" :label="item.title" :value="item.id">
					</el-option>
				</el-select>
				<!-- <el-select v-model="searchParam.status" placeholder="请选择认证状态" style="width:200px;margin-left: 10px;">
				    <el-option v-for="(item,index) in statusList" :key="index" :label="item" :value="index">
				    </el-option>
				</el-select>
                <el-date-picker style="width:340px" clearable class="ml-10" v-model="searchParam.search_time"
                    value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker> -->
				<div class="ml-10">
					<el-button type="primary" @click="searchinfo">搜索</el-button>
					<el-button type="primary" @click="reset">重置</el-button>
				</div>
			</div>

			<div style="display: flex;">
				<el-button size="mini" v-for="(item, index) in buttons" :key="index" :type="item.path | formatType"
					:icon="item.icon" :disabled="item.path | formatDisabled(selection)" @click="tapButton(item)">{{
						item.title }}</el-button>
				<!-- <el-button type="primary" @click="add()">添加</el-button> -->
				<!-- <el-upload-file :showfile="false" v-model="importindex"  @click="importExcel()"
				accept=".xlsx" style="margin: 0 10px;" title="导入"></el-upload-file> -->
				<!-- <eluploadfile v-if="$auth('import')" :auto-upload="false" :upload-clear="true" :show-file-list="false" @change="upload" :limit="1" :multiple="false" accept=".xls,.xlsx" class="ml-10">
				    <el-button type="primary" >导入</el-button>
				</eluploadfile> -->
				<!-- <el-button type="primary" @click="exportExcel">导出</el-button> -->
				<!-- <el-button style="margin-left: 10px;"  type="danger" @click="selectdel"
					:disabled="!selection.length">批量删除</el-button> -->
			</div>
		</div>
		<el-table v-if="list.length" ref="table" show-summary :summary-method="getSummaries" class="main-table" header-align="center" border
			:data="list" @selection-change="onSelectionChange" height="100%" row-key="id"
			header-cell-class-name="main-table-header" @row-click="onRowClick">
			<el-table-column type="selection" width="50"> </el-table-column>
			<!-- <el-table-column  show-overflow-tooltip label="社区名称" prop="mass_name"></el-table-column> -->
			<!-- <el-table-column label="门头照片">
				<template slot-scope="{row}">
					<el-image style="width:60px;height:60px" :src="row.img.split(',')[0] " :preview-src-list="[row.img.split(',')[0]]">
					</el-image>
				</template>
			</el-table-column> -->
			<!-- <el-table-column v-if="$route.query.type" show-overflow-tooltip label="联系方式" prop="mass_mobile" width="120px"></el-table-column> -->
			<el-table-column show-overflow-tooltip label="用户名称" prop="name"></el-table-column>
			<!-- <el-table-column show-overflow-tooltip label="状态" prop="status" > -->
			<!-- 社区审核0待审核1通过2拒绝 -->
			<!-- <template slot-scope="{row}">
					<el-tag v-if="row.status==0">待审核</el-tag>
					<el-tag  v-if="row.status==1" type="success">已通过</el-tag>
					<el-tag  v-if="row.status==2" type="info">已拒绝</el-tag>
				</template>
			</el-table-column> -->

			<el-table-column show-overflow-tooltip label="来源" prop="memo"></el-table-column>
			<el-table-column show-overflow-tooltip label="变更积分" prop="score"></el-table-column>
			<el-table-column show-overflow-tooltip label="变更前积分" prop="before"></el-table-column>
			<el-table-column show-overflow-tooltip label="变更后积分" prop="after"></el-table-column>
			<el-table-column show-overflow-tooltip label="类型" prop="type">
				<template slot-scope="{row}">
					<!-- 类型1加积分 2减积分 -->
					<el-tag v-if="row.type == 1">增加积分</el-tag>
					<el-tag v-if="row.type == 2" type="success">扣除积分</el-tag>
				</template>
			</el-table-column>
			<!-- 	<el-table-column label="生日" prop="birthday">
				<template slot-scope="{row}">
					{{ row.birthday | formatDate }}
				</template>
			</el-table-column> -->
			<el-table-column label="入职时间" prop="createtime">
				<template slot-scope="{row}">
					{{ row.createtime | formatDate }}
				</template>
			</el-table-column>
			<!-- <el-table-column label="操作">
				<template slot-scope="{row}"> -->
			<!-- 社区审核0待审核1通过2拒绝 -->
			<!-- <el-button  v-if="row.status==2" type="primary" @click.stop="add(row,2)">重新提交</el-button>
					<el-button  v-if="row.status==1||row.status==0" type="primary" @click.stop="add(row,1)">详情</el-button>
					<el-button  v-if="row.status==0&&(myid==1||myid==9)" type="warning" @click.stop="examine(row)">审核</el-button>
				</template>
			</el-table-column> -->
		</el-table>
		<el-pagination class="main-pagination" :current-page.sync="searchParam.page" :page-size="searchParam.limit"
			:total="searchParam.total" @current-change="onCurrentChange" layout="total,jumper,prev,pager,next">
		</el-pagination>
		<el-dialog :title="title" :key="formDataStatus" :visible.sync="centerDialogVisible" @close="onDialogClose"
			width="60%">
			<add @close="close" :formData="userinfo" @search="addcommunity" :isdisabled="isdisabled"></add>
		</el-dialog>
	</div>
</template>
<script>
// import companyCate from '@/components/company-cate.vue'
// import supervisorSelect from '@/components/supervisor-select.vue'auth
import add from './components/add.vue'
import { imghttp, get, post, del } from '@/assets/js/http'
import shequSelect from '@/components/shequ-select.vue'
// import eluploadfile from '../../components/el-upload-file.vue'
export default {
	components: { add,shequSelect },
	data() {
		return {
			list: [],
			selection: [],
			addShow: false, //添加显示
			centerDialogVisible: false, //详情弹窗
			title: '', //弹窗标题
			userinfo: {}, //用户信息
			typeList: [{
				id: 1,
				title: '增加积分'
			}, {
				id: 2,
				title: '扣除积分'
			}], //类型1增加积分2扣除积分
			statusList: ['未认证', '认证待审核', '认证通过', '认证驳回'], //认证状态 0未认证 1认证待审核 2认证通过 3认证驳回
			searchParam: {
				page: 1,
				limit: 20,
				total: 0,
				cadre_id: '',
				search_time: [],
				status: '',
				type: '',
				search: '',
			},
			searchParamCp: {
				status: '',
			},
			formDataStatus: false,
			imgs: [],
			isdisabled: false,
			myid: '',//1超管2课程供应商6产品供应商8社区管理员9总审核员
			resAll:null
		}
	},
	computed: {
		buttons() {
			return this.$store.state.buttons;
		}
	},
	created() {
		Object.assign(this.searchParam, this.$route.query ? this.$route.query : {})
		Object.assign(this.searchParamCp, this.$route.query ? this.$route.query : {})
		this.getList()
		this.getinfo()
	},
	watch: {
		'$route': {
			deep: true,
			handler() {
				this.reset()
			}
		},
	},
	methods: {
		getSummaries(param) {
			const { columns, data } = param;
			const sums = [];
			columns.forEach((column, index) => {
				if (index === 0) {
					sums[index] = '';
					return;
				}
				if(index===3){
					sums[index] = '合计变更积分:'+this.resAll.score
				}
				if(index===4){
					sums[index] = '合计变更前积分:'+this.resAll.before_score
				}
				if(index===5){
					sums[index] = '合计变更后积分:'+this.resAll.after_score
				}
			});
				return sums;
		},
		//获取社区列表
		async getList() {
			this.$loading()
			// let { list,total } = 
			console.log(this.searchParam.search_time)
			await get({
				url: '/admin/user.User/userScore',
				params: {
					...this.searchParam,
					...{
						server_type: this.$route.query.type ? 2 : 1,
						search_time: this.searchParam.search_time.length ? this.searchParam.search_time.map(item => item / 1000) : ''
					}
				}
			}).then(res => {
				this.list = res.list.list
				this.searchParam.total = res.list.total
				this.resAll = res.list
				this.importindex = []
				this.$loading().close()
			})
		},
		//获取权限
		getinfo() {
			post({
				url: '/admin/project.Activity/admin_info',
			}).then(res => {
				this.myid = res.role
				console.log('myid', this.myid, res.id)
			})
		},
		//添加/编辑弹窗
		add(row, n) {
			if (row) { //编辑
				this.centerDialogVisible = true
				if (n == 1) {
					this.title = "社区信息"
					this.isdisabled = true
				} else {
					this.title = "编辑社区信息"
					this.isdisabled = false
				}
				get({
					url: '/admin/project.Community/edit',
					params: {
						id: row.id
					}
				}).then(res => {
					// this.userinfo = res.row
					// console.log(this.userinfo.img,'this.userinfo.img',res.row)
					// this.userinfo.img = [{
					// 	name: res.row.img,
					// 	url: res.row.img,
					// }]
					const form = JSON.parse(JSON.stringify(res.row))
					let imgs = []
					form.img = form.img.split(',')
					form.img.forEach(item => {
						imgs.push({ name: item, url: item })
					})
					form.img = imgs
					form.location = { lat: form.latitude, lng: form.longitude }
					this.userinfo = form
					// this.isdisabled=true
					this.userinfo.location = { lat: this.userinfo.latitude, lng: this.userinfo.longitude }
					console.log(this.userinfo.img, 'this.userinfo.img')
				})

			} else { //添加
				this.userinfo = {}
				this.isdisabled = false
				this.userinfo.location = {}
				this.centerDialogVisible = true
				this.title = "添加社区信息"
			}
			this.formDataStatus = true;
		},
		//添加社区信息
		addcommunity(result) {
			const info = JSON.parse(JSON.stringify(result))
			// const formData = JSON.parse(JSON.stringify(this.formData));
			info.latitude = info.location.lat
			info.longitude = info.location.lng
			info.img.forEach(item => {
				this.imgs.push(item.url)
			})
			info.img = this.imgs.join(',')
			info.desc = ''
			console.log(info, info.img, this.imgs)
			// return
			if (info.id) {
				info.status = 0//社区审核0待审核1通过2拒绝
				post({
					url: '/admin/project.Community/edit',
					data: info,
					successTip: true,
				}).then(res => {
					this.close()
					this.getList()
				})
			} else {
				post({
					url: '/admin/project.Community/add',
					data: info,
					successTip: true,
				}).then(res => {
					this.close()
					this.getList()
				})
			}
		},
		//关闭弹窗
		close() {
			this.centerDialogVisible = false
		},
		reset() {
			this.searchParamCp = this.$options.data().searchParam
			this.searchParam = this.$options.data().searchParam
			this.getList()
		},
		searchinfo() {
			this.searchParam.page = 1
			this.searchParam = {
				...this.searchParam,
				...this.searchParamCp
			}
			this.getList()
		},
		onCurrentChange() {
			this.getList()
		},
		onRowClick(row) {
			this.$refs.table.toggleRowSelection(row);
		},
		onSelectionChange(selection) {
			this.selection = selection;
		},
		selectdel() {
			this.$confirm(
				`此操作将永久删除该${this.selection.length}个社区, 是否继续?`,
				"提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}
			)
				.then(async () => {
					let query = "?";
					for (let i = 0; i < this.selection.length; i++) {
						const item = this.selection[i];
						if (i > 0) {
							query += "&ids[]=" + item.id;
						} else {
							query += "ids[]=" + item.id;
						}
					}
					await del({
						url: "/admin/project.Community/del" + query,
						successTip: true,
						loading: true
					});
					this.getList()
				})
				.catch(() => {
					this.$refs.table.clearSelection();
				});
		},
		//审核
		examine(row) {
			this.$confirm('是否通过该社区申请?', '提示', {
				confirmButtonText: '通过',
				cancelButtonText: '拒绝',
				type: 'warning'
			}).then(() => {
				post({
					url: '/admin/project.Community/apply_status',
					data: {
						id: row.id,
						status: 1,//社区审核0待审核1通过2拒绝
					},
					successTip: true,
				}).then(res => {
					this.getList()
				})
			}).catch(action => {
				if (action === 'cancel') {
					post({
						url: '/admin/project.Community/apply_status',
						data: {
							id: row.id,
							status: 2,//社区审核0待审核1通过2拒绝
						},
						successTip: true,
					}).then(res => {
						this.getList()
						// this.$message({
						// 	type: 'info',
						// 	message: '已取消删除'
						// });  
					})
				}
				else {

				}

			});
		},
		//导出
		async exportExcel() {
			this.$loading()
			let res = await get({
				export: true,
				url: '/api/Realmerit/score_export',
				params: {
					...this.searchParam,
					...{
						server_type: this.$route.query.type ? 2 : 1,
						search_time: this.searchParam.search_time.length ? this.searchParam.search_time.map(item => item / 1000) : ''
					}
				}
			})
			let a = document.createElement('a');
			var url = URL.createObjectURL(res);
			a.href = url;
			a.click();
			this.$loading().close()
		},
		//导入
		async upload({
			raw
		}) {
			this.$loading({
				text: '导入中 请等待...'
			})

			await imghttp({
				url: '/admin/basic.Masses/import',
				data: {
					file: raw
				},
				successTip: true
			})
			this.getList()
			this.$loading().close()
		},
		onDialogClose() {

		},
		tapButton(item) {
			if (item.path === "add") {
				this.add();
			} else if (item.path === "edit") {
				this.onEdit();
			} else if (item.path === "delete") {
				this.selectdel();
			} else if (item.path === "search") {
				this.onSearch();
			} else if (item.path === "export") {
				this.exportExcel();
			}
		},

	}
}
</script>

<style>
.el-date-editor .el-range-separator {
	padding: 0;
}
</style>
